﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCCN.BLL;
using SCCN.DAL;
using System.Web.UI.HtmlControls;
using SCCN.Utility;
using System.Data;

public partial class Admin_ArticleList : System.Web.UI.Page
{
    ArticleBLL bllArticle = ArticleBLL.getInstance();
    CategoryBLL bllCategory = CategoryBLL.getInstance();
    SysUserBLL bllSysUser = SysUserBLL.getInstance();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp();
            BindRpt();
        }
    }

    void BindDrp()
    {
        this.drpCategory.DataSource = bllCategory.GetCategory();
        this.drpCategory.DataTextField = "CategoryName";
        this.drpCategory.DataValueField = "Id";
        this.drpCategory.DataBind();
        this.drpCategory.Items.Insert(0, new ListItem("请选择", "0"));
        this.drpCategory.SelectedValue = HttpHelper.GetParameters("categoryid", HttpHelper.MethodType.GET);
    }

    void BindRpt()
    {
        SC_Article obj = new SC_Article();
        if (drpCategory.SelectedItem.Text != "请选择")
        {
            obj.CategoryId = Convert.ToInt32(this.drpCategory.SelectedValue);
        }
        if (this.txtKeyWord.Text.Length > 0)
        {
            obj.KeyWord = this.txtKeyWord.Text;
        }
        else
        {
            obj.KeyWord = string.Empty;
        }
        if (this.txtTitle.Text.Length > 0)
        {
            obj.ArticleTitle = this.txtTitle.Text;
        }
        else
        {
            obj.ArticleTitle = string.Empty;
        }
        if (this.txtTag.Text.Length > 0)
        {
            obj.Tag = this.txtTag.Text;
        }
        else
        {
            obj.Tag = string.Empty;
        }
        if (this.drpIsChoice.SelectedItem.Text != "请选择")
        {
            obj.IsChoice = this.drpIsChoice.SelectedValue == "1" ? true : false;
        }
        if (this.drpIsHot.SelectedItem.Text != "请选择")
        {
            obj.IsHot = this.drpIsHot.SelectedValue == "1" ? true : false;
        }
        if (this.drpIsTag.SelectedItem.Text != "请选择")
        {
            obj.IsTag = this.drpIsTag.SelectedValue == "1" ? true : false;
        }
        int rowCount = 0;
        this.rptList.DataSource = bllArticle.GetArticleByCondition(obj, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out rowCount);
        this.rptList.DataBind();
        this.AspNetPager1.RecordCount = rowCount;
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindRpt();
    }

    public string GetUser(int userId)
    {
        return bllSysUser.GetUserByUserId(userId).UserName;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddArticle.aspx");
    }


    /// <summary>
    /// 批量生成
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        bool result = false;
        string ids = string.Empty;
        SC_Article obj;
        foreach (RepeaterItem ri in this.rptList.Items)
        {
            HiddenField Hid = ri.FindControl("Hid") as HiddenField;
            CheckBox ckb = ri.FindControl("ckbSel") as CheckBox;
            if (ckb.Checked)
            {
                CreateArticle.articleId = Convert.ToInt32(Hid.Value);
                result = CreateArticle.Create();
                if (!result)
                {
                    ids += Hid.Value + ",";
                }
                else
                {
                    obj = bllArticle.GetSC_ArticleById(Convert.ToInt32(Hid.Value));
                    obj.CreateUrl = "/article/" + Hid.Value + ".shtml";
                    obj.PublishDate = DateTime.Now;
                    bllArticle.Update(obj);
                }
            }
        }
        if (!result)
        {
            ids = ids.Remove(ids.Length - 1);
            HttpHelper.Alert("生成失败，Id为：" + ids + "的资讯生成失败！", Page);
        }
    }


    /// <summary>
    /// Reapeater命令事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = 0;
        int.TryParse(e.CommandArgument.ToString(), out id);
        if (e.CommandName == "Del")
        {
            if (bllArticle.Delete(id))
            {
                HttpHelper.Alert("删除成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("删除失败！", Page);
            }
        }
        if (e.CommandName == "Update")
        {
            Response.Redirect("AddArticle.aspx?id=" + id);
        }
        if (e.CommandName == "Hot")
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(id);
            obj.IsHot = true;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("更新成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("更新失败！", Page);
            }
        }
        if (e.CommandName == "Choice")
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(id);
            obj.IsChoice = true;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("更新成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("更新失败！", Page);
            }
        }
        if (e.CommandName == "Tag")
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(id);
            obj.IsTag = true;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("更新成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("更新失败！", Page);
            }
        }
        if (e.CommandName == "UnHot")
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(id);
            obj.IsHot = false;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("更新成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("更新失败！", Page);
            }
        }
        if (e.CommandName == "UnChoice")
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(id);
            obj.IsChoice = false;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("更新成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("更新失败！", Page);
            }
        }
        if (e.CommandName == "UnTag")
        {
            SC_Article obj = bllArticle.GetSC_ArticleById(id);
            obj.IsTag = false;
            if (bllArticle.Update(obj))
            {
                HttpHelper.Alert("更新成功！", Page);
                BindRpt();
            }
            else
            {
                HttpHelper.Alert("更新失败！", Page);
            }
        }
    }


    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            SC_Article rv = (SC_Article)e.Item.DataItem;
            Button btnSetHot = (Button)e.Item.FindControl("btnSetHot");
            Button btnSetTag = (Button)e.Item.FindControl("btnSetTag");
            Button btnSetChoice = (Button)e.Item.FindControl("btnSetChoice");
            Button btnUnSetHot = (Button)e.Item.FindControl("btnUnSetHot");
            Button btnUnSetTag = (Button)e.Item.FindControl("btnUnSetTag");
            Button btnUnSetChoice = (Button)e.Item.FindControl("btnUnSetChoice");
            if (Convert.ToBoolean(rv.IsHot))
            {
                btnSetHot.Visible = false;
                btnUnSetHot.Visible = true;
            }
            else
            {
                btnSetHot.Visible = true;
                btnUnSetHot.Visible = false;
            }
            if (Convert.ToBoolean(rv.IsTag))
            {
                btnSetTag.Visible = false;
                btnUnSetTag.Visible = true;
            }
            else
            {
                btnSetTag.Visible = true;
                btnUnSetTag.Visible = false;
            }
            if (Convert.ToBoolean(rv.IsChoice))
            {
                btnSetChoice.Visible = false;
                btnUnSetChoice.Visible = true;
            }
            else
            {
                btnSetChoice.Visible = true;
                btnUnSetChoice.Visible = false;
            }
        }
    }


    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSel_CheckedChanged(object sender, EventArgs e)
    {
        bool result = false;
        string articleIds = string.Empty;
        foreach (RepeaterItem ri in this.rptList.Items)
        {
            CheckBox ckbselect = ri.FindControl("ckbSel") as CheckBox;

            if (this.ckbAllSel.Checked)
            {
                ckbselect.Checked = true;
            }
            else
            {
                ckbselect.Checked = false;
            }
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindRpt();
    }
}